package com.cn.mmd.controller;


import com.cn.mmd.model.StudentIntegral;
import com.cn.mmd.model.StudentIntegralLog;
import com.cn.mmd.service.EvaluateTeacherService;
import com.cn.mmd.service.StudentIntegralLogService;
import com.cn.mmd.service.StudentIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2016/12/7.
 */
@Controller
@RequestMapping(value = "page/studentIntegral")
public class StudentIntegralController {

    @Autowired
    private StudentIntegralService studentIntegralService;

    @Autowired
    private StudentIntegralLogService studentIntegralLogService;

    @Autowired
    EvaluateTeacherService evaluateTeacherService;

    /**
     * 添加积分
     * @param id
     * @param type
     * @param n
     * @return
     */
    public synchronized Object addIntegral(String id, String type, int n) {
        int num = 0;
        int num1 = 200;//升1级需要的分数
        StudentIntegral studentIntegral = new StudentIntegral();
        StudentIntegral studentIntegral1 = studentIntegralService.selectByPrimaryKey(id);
        Date date = new Date();
        Long time = date.getTime();
        StudentIntegralLog studentIntegralLog1 = new StudentIntegralLog();
        if (type.equals("login")) {//登陆
            SimpleDateFormat time2 = new SimpleDateFormat("yyyy-MM-dd");
            String timec = time2.format(date);
            studentIntegralLog1.setStudentid(id);
            studentIntegralLog1.setGradesource(type);
            studentIntegralLog1.setTime(timec);
            if (studentIntegralLogService.selectByAll(studentIntegralLog1) != null && studentIntegralLogService.selectByAll(studentIntegralLog1).size() == 0) {
                if (studentIntegral1 != null) {
                    studentIntegral.setStudentid(id);
                    studentIntegral.setLogingradesum(studentIntegral1.getLogingradesum() + n);
                    studentIntegral.setGradesum(studentIntegral1.getGradesum() + n);
                    if (studentIntegral1.getScore() + n >= num1) {
                        studentIntegral.setScore(studentIntegral1.getScore() + n - num1);
                        studentIntegral.setGrade(studentIntegral1.getGrade() + 1);
                        studentIntegral.setLotterynumber(studentIntegral1.getLotterynumber() + 1);
                    } else {
                        studentIntegral.setScore(studentIntegral1.getScore() + n);
                    }
                    num = studentIntegralService.updateByPrimaryKeySelective(studentIntegral);
                } else {
                    studentIntegral.setStudentid(id);
                    studentIntegral.setLogingradesum(n+5);
                    studentIntegral.setGradesum(n+5);
                    studentIntegral.setUpgrade(num1);
                    studentIntegral.setScore(n+5);
                    num = studentIntegralService.insertSelective(studentIntegral);
                    StudentIntegralLog studentIntegralLog = new StudentIntegralLog();
                    SimpleDateFormat time1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String logTime = time1.format(date);
                    studentIntegralLog.setTime(logTime);
                    studentIntegralLog.setStudentid(id);
                    studentIntegralLog.setGradesource("install");
                    studentIntegralLog.setGradenum(5);
                    studentIntegralLogService.insertSelective(studentIntegralLog);
                }
                if (num > 0) {
                    StudentIntegralLog studentIntegralLog = new StudentIntegralLog();
                    SimpleDateFormat time1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String logTime = time1.format(date);
                    studentIntegralLog.setTime(logTime);
                    studentIntegralLog.setStudentid(id);
                    studentIntegralLog.setGradesource(type);
                    studentIntegralLog.setGradenum(n);
                    if (studentIntegralLogService.insertSelective(studentIntegralLog) > 0) {
                        return num;
                    }
                }
            }
        } else if (type.equals("addActivity")) {//发布活动
            studentIntegralLog1.setStudentid(id);
            studentIntegralLog1.setGradesource(type);
            if(studentIntegralLogService.selectByAll(studentIntegralLog1)!=null&&studentIntegralLogService.selectByAll(studentIntegralLog1).size()>0) {
                try {
                    SimpleDateFormat time2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    StudentIntegralLog studentIntegralLog2 = studentIntegralLogService.selectByAll(studentIntegralLog1).get(0);
                    Date date1 = time2.parse(studentIntegralLog2.getTime());
                    long unixTimestamp = date1.getTime() / 1000 / 60 / 60 / 24;
                    long unixTimestamp1 = time / 1000 / 60 / 60 / 24;
                    if (unixTimestamp1 - unixTimestamp >= 7) {
                        if (studentIntegral1 != null) {
                            studentIntegral.setStudentid(id);
                            studentIntegral.setActivitygradesum(studentIntegral1.getActivitygradesum() + n);
                            studentIntegral.setGradesum(studentIntegral1.getGradesum() + n);
                            if (studentIntegral1.getScore() + n >= num1) {
                                studentIntegral.setScore(studentIntegral1.getScore() + n - num1);
                                studentIntegral.setGrade(studentIntegral1.getGrade() + 1);
                                studentIntegral.setLotterynumber(studentIntegral1.getLotterynumber() + 1);
                            } else {
                                studentIntegral.setScore(studentIntegral1.getScore() + n);
                            }
                            num = studentIntegralService.updateByPrimaryKeySelective(studentIntegral);
                        } else {
                            studentIntegral.setStudentid(id);
                            studentIntegral.setActivitygradesum(n);
                            studentIntegral.setGradesum(n);
                            studentIntegral.setUpgrade(num1);
                            studentIntegral.setScore(n);
                            num = studentIntegralService.insertSelective(studentIntegral);
                        }
                        if (num > 0) {
                            StudentIntegralLog studentIntegralLog = new StudentIntegralLog();
                            SimpleDateFormat time1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            String logTime = time1.format(date);
                            studentIntegralLog.setTime(logTime);
                            studentIntegralLog.setGradesource(type);
                            studentIntegralLog.setStudentid(id);
                            studentIntegralLog.setGradenum(n);
                            if (studentIntegralLogService.insertSelective(studentIntegralLog) > 0) {
                                return num;
                            }
                        }
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }else {
                if (studentIntegral1 != null) {
                    studentIntegral.setStudentid(id);
                    studentIntegral.setActivitygradesum(studentIntegral1.getActivitygradesum() + n);
                    studentIntegral.setGradesum(studentIntegral1.getGradesum() + n);
                    if (studentIntegral1.getScore() + n >= num1) {
                        studentIntegral.setScore(studentIntegral1.getScore() + n - num1);
                        studentIntegral.setGrade(studentIntegral1.getGrade() + 1);
                        studentIntegral.setLotterynumber(studentIntegral1.getLotterynumber() + 1);
                    } else {
                        studentIntegral.setScore(studentIntegral1.getScore() + n);
                    }
                    num = studentIntegralService.updateByPrimaryKeySelective(studentIntegral);
                } else {
                    studentIntegral.setStudentid(id);
                    studentIntegral.setActivitygradesum(n);
                    studentIntegral.setGradesum(n);
                    studentIntegral.setUpgrade(num1);
                    studentIntegral.setScore(n);
                    num = studentIntegralService.insertSelective(studentIntegral);
                }
                if (num > 0) {
                    StudentIntegralLog studentIntegralLog = new StudentIntegralLog();
                    SimpleDateFormat time1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String logTime = time1.format(date);
                    studentIntegralLog.setTime(logTime);
                    studentIntegralLog.setGradesource(type);
                    studentIntegralLog.setStudentid(id);
                    studentIntegralLog.setGradenum(n);
                    if (studentIntegralLogService.insertSelective(studentIntegralLog) > 0) {
                        return num;
                    }
                }
            }
        } else if (type.equals("answer")) {//回答问题
            SimpleDateFormat time2 = new SimpleDateFormat("yyyy-MM-dd");
            String timec = time2.format(date);
            studentIntegralLog1.setTime(timec);
            studentIntegralLog1.setStudentid(id);
            studentIntegralLog1.setGradesource(type);
            if (studentIntegralLogService.selectByAll(studentIntegralLog1) != null && studentIntegralLogService.selectByAll(studentIntegralLog1).size() < 2) {
                if (studentIntegral1 != null) {
                    studentIntegral.setStudentid(id);
                    studentIntegral.setAnswergradesum(studentIntegral1.getAnswergradesum() + n);
                    studentIntegral.setGradesum(studentIntegral1.getGradesum() + n);
                    if (studentIntegral1.getScore() + n >= num1) {
                        studentIntegral.setScore(studentIntegral1.getScore() + n - num1);
                        studentIntegral.setGrade(studentIntegral1.getGrade() + 1);
                        studentIntegral.setLotterynumber(studentIntegral1.getLotterynumber() + 1);
                    } else {
                        studentIntegral.setScore(studentIntegral1.getScore() + n);
                    }
                    num = studentIntegralService.updateByPrimaryKeySelective(studentIntegral);
                } else {
                    studentIntegral.setStudentid(id);
                    studentIntegral.setAnswergradesum(n);
                    studentIntegral.setGradesum(n);
                    studentIntegral.setUpgrade(num1);
                    studentIntegral.setScore(n);
                    num = studentIntegralService.insertSelective(studentIntegral);
                }
                if (num > 0) {
                    StudentIntegralLog studentIntegralLog = new StudentIntegralLog();
                    SimpleDateFormat time1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String logTime = time1.format(date);
                    studentIntegralLog.setTime(logTime);
                    studentIntegralLog.setGradesource(type);
                    studentIntegralLog.setStudentid(id);
                    studentIntegralLog.setGradenum(n);
                    if (studentIntegralLogService.insertSelective(studentIntegralLog) > 0) {
                        return num;
                    }
                }
            }
        } else if (type.equals("questions")) {//提问
            SimpleDateFormat time2 = new SimpleDateFormat("yyyy-MM-dd");
            String timec = time2.format(date);
            studentIntegralLog1.setTime(timec);
            studentIntegralLog1.setStudentid(id);
            studentIntegralLog1.setGradesource(type);
            if (studentIntegralLogService.selectByAll(studentIntegralLog1) != null && studentIntegralLogService.selectByAll(studentIntegralLog1).size() < 2) {
                if (studentIntegral1 != null) {
                    studentIntegral.setStudentid(id);
                    studentIntegral.setQuestionsgradesum(studentIntegral1.getQuestionsgradesum() + n);
                    studentIntegral.setGradesum(studentIntegral1.getGradesum() + n);
                    studentIntegral.setScore(studentIntegral1.getScore() + n);
                    if (studentIntegral1.getScore() + n >= num1) {
                        studentIntegral.setScore(studentIntegral1.getScore() + n - num1);
                        studentIntegral.setGrade(studentIntegral1.getGrade() + 1);
                        studentIntegral.setLotterynumber(studentIntegral1.getLotterynumber() + 1);
                    } else {
                        studentIntegral.setScore(studentIntegral1.getScore() + n);
                    }
                    num = studentIntegralService.updateByPrimaryKeySelective(studentIntegral);
                } else {
                    studentIntegral.setStudentid(id);
                    studentIntegral.setQuestionsgradesum(n);
                    studentIntegral.setGradesum(n);
                    studentIntegral.setUpgrade(num1);
                    studentIntegral.setScore(n);
                    num = studentIntegralService.insertSelective(studentIntegral);
                }
                if (num > 0) {
                    StudentIntegralLog studentIntegralLog = new StudentIntegralLog();
                    SimpleDateFormat time1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String logTime = time1.format(date);
                    studentIntegralLog.setTime(logTime);
                    studentIntegralLog.setGradesource(type);
                    studentIntegralLog.setStudentid(id);
                    studentIntegralLog.setGradenum(n);
                    if (studentIntegralLogService.insertSelective(studentIntegralLog) > 0) {
                        return num;
                    }
                }
            }
        } else if (type.equals("evaluate")) {//评价
            if (studentIntegral1 != null) {
                studentIntegral.setStudentid(id);
                studentIntegral.setEvaluategradesum(studentIntegral1.getEvaluategradesum() + n);
                studentIntegral.setGradesum(studentIntegral1.getGradesum() + n);
                studentIntegral.setScore(studentIntegral1.getScore() + n);
                if (studentIntegral1.getScore() + n >= num1) {
                    studentIntegral.setScore(studentIntegral1.getScore() + n - num1);
                    studentIntegral.setGrade(studentIntegral1.getGrade() + 1);
                    studentIntegral.setLotterynumber(studentIntegral1.getLotterynumber() + 1);
                } else {
                    studentIntegral.setScore(studentIntegral1.getScore() + n);
                }
                num = studentIntegralService.updateByPrimaryKeySelective(studentIntegral);
            } else {
                studentIntegral.setStudentid(id);
                studentIntegral.setEvaluategradesum(n);
                studentIntegral.setGradesum(n);
                studentIntegral.setUpgrade(num1);
                studentIntegral.setScore(n);
                num = studentIntegralService.insertSelective(studentIntegral);
            }
            if (num > 0) {
                StudentIntegralLog studentIntegralLog = new StudentIntegralLog();
                SimpleDateFormat time1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String logTime = time1.format(date);
                studentIntegralLog.setTime(logTime);
                studentIntegralLog.setGradesource(type);
                studentIntegralLog.setStudentid(id);
                studentIntegralLog.setGradenum(n);
                if (studentIntegralLogService.insertSelective(studentIntegralLog) > 0) {
                    return num;
                }
            }
        } else if (type.equals("signIn")) {//签到
            SimpleDateFormat time2 = new SimpleDateFormat("yyyy-MM-dd");
            String timec = time2.format(date);
            studentIntegralLog1.setTime(timec);
            studentIntegralLog1.setStudentid(id);
            studentIntegralLog1.setGradesource(type);
            if (studentIntegralLogService.selectByAll(studentIntegralLog1) != null && studentIntegralLogService.selectByAll(studentIntegralLog1).size() < 1) {
                if (studentIntegral1 != null) {
                    studentIntegral.setStudentid(id);
                    studentIntegral.setSigningradesum(studentIntegral1.getSigningradesum() + n);
                    studentIntegral.setGradesum(studentIntegral1.getGradesum() + n);
                    if (studentIntegral1.getScore() + n >= num1) {
                        studentIntegral.setScore(studentIntegral1.getScore() + n - num1);
                        studentIntegral.setGrade(studentIntegral1.getGrade() + 1);
                        studentIntegral.setLotterynumber(studentIntegral1.getLotterynumber() + 1);
                    } else {
                        studentIntegral.setScore(studentIntegral1.getScore() + n);
                    }
                    num = studentIntegralService.updateByPrimaryKeySelective(studentIntegral);
                } else {
                    studentIntegral.setStudentid(id);
                    studentIntegral.setSigningradesum(n);
                    studentIntegral.setGradesum(n);
                    studentIntegral.setUpgrade(num1);
                    studentIntegral.setScore(n);
                    num = studentIntegralService.insertSelective(studentIntegral);
                }
                if (num > 0) {
                    StudentIntegralLog studentIntegralLog = new StudentIntegralLog();
                    SimpleDateFormat time1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String logTime = time1.format(date);
                    studentIntegralLog.setTime(logTime);
                    studentIntegralLog.setGradesource(type);
                    studentIntegralLog.setStudentid(id);
                    studentIntegralLog.setGradenum(n);
                    if (studentIntegralLogService.insertSelective(studentIntegralLog) > 0) {
                        return num;
                    }
                }
            }
        }
        return null;
    }
}
